package com.sky.task;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
public class OrderTask {
    @Autowired
    private OrderMapper orderMapper;
    @Scheduled(cron = "0 0/5 * * * ?")
     public void processTimeoutOrder(){
        log.info("开始处理超时订单--{}",LocalDateTime.now());
         LocalDateTime time = LocalDateTime.now().plusMinutes(-15);
         UpdateWrapper<Orders> wrapper = new UpdateWrapper<>();
         wrapper.eq("status",Orders.PENDING_PAYMENT)
                 .lt("order_time",time)
                 .set("status",Orders.CANCELLED)
                 .set("cancel_time",LocalDateTime.now())
                 .set("cancel_reason","订单超时，自动取消");
         orderMapper.update(wrapper);
     }
    @Scheduled(cron = "0 0 1 * * ?")
     public void processDeliveryOrder(){
        log.info("开始处理未配送的订单--{}",LocalDateTime.now());
        LocalDateTime time = LocalDateTime.now().plusMinutes(-60);
        UpdateWrapper<Orders> wrapper = new UpdateWrapper<>();
        wrapper.eq("status",Orders.DELIVERY_IN_PROGRESS)
                .lt("order_time",time)
                .set("status",Orders.COMPLETED);
        orderMapper.update(wrapper);
     }

}
